home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 SRC / Tools / bgen / test.py < prev   
Text File  |  1996-03-20  |  4KB  |  156 lines

  1. # This is an absolutely horrid piece of code.
  2. # I wrote it to exercise the (mostly automatically generated) interfaces to the
  3. # Mac Toolbox that I was in the midst of creating.
  4. # It consists of a fairly complete Main loop -- it handles most of the stuff
  5. # that every application should have:
  6. # - moving windows
  7. # - resizing windows
  8. # - hanling the zoombox
  9. # - handling the close box
  10. # - handling the apple menu (including starting desk acc's)
  11. # - key events (including key repeat)
  12. # - update events
  13. # It also detects clicks in controls and tracks the control.
  14.  
  15. from Evt import *
  16. from Win import *
  17. from Dlg import *
  18. from Ctl import *
  19. from Menu import *
  20. from Qd import *
  21. from Res import *
  22. import AE
  23.  
  24. from Events import *
  25. from Windows import *
  26. from Controls import *
  27.  
  28. import MacOS
  29. MacOS.EnableAppswitch(0) # Stop Python from ever getting events itself
  30.  
  31. everywhere = (-32000, -32000, 32000, 32000)
  32. # NB using 0x7fff doesn't work -- apparently a little slop must be present
  33.  
  34. print "There are now", len(dir()), "symbols in the name space!"
  35.  
  36. def main():
  37.     appleid = 1
  38.     ClearMenuBar()
  39.     applemenu = NewMenu(appleid, "\024")
  40.     applemenu.AppendMenu("All about me...;(-")
  41.     applemenu.AppendResMenu('DRVR')
  42.     applemenu.InsertMenu(0)
  43.     DrawMenuBar()
  44.     
  45.     winwidth = 200
  46.     winheight = 100
  47.     r = (30, 60, 30+winwidth, 60+winheight)
  48.     w = NewWindow(r, "Hello world", 1, zoomDocProc, -1, 1, 0)
  49.     resizerect = (10, 20, 70, 40)
  50.     resize = NewControl(w, resizerect, "Resize", 1, 0, 0, 1, pushButProc, 0)
  51.     quitrect = (90, 20, 130, 40)
  52.     quit = NewControl(w, quitrect, "Quit", 1, 0, 0, 1, pushButProc, 0)
  53.     while 1:
  54.         doit, event = WaitNextEvent(-1,0)
  55.         if doit:
  56.             SetPort(w)
  57.             (what, message, when, where, modifiers) = event
  58.             if what == mouseDown:
  59.                 partcode, win = FindWindow(where)
  60.                 if partcode == inDrag: # transvestites, take note
  61.                     win.DragWindow(where, everywhere)
  62.                 elif partcode == inGoAway:
  63.                     if win.TrackGoAway(where):
  64.                         break
  65.                 elif partcode in (inZoomIn, inZoomOut):
  66.                     SetPort(win) # !!!
  67.                     if win.TrackBox(where, partcode):
  68.                         win.ZoomWindow(partcode, 1)
  69.                 elif partcode in (inSysWindow, inDesk):
  70.                     SystemClick(event, win)
  71.                 elif partcode == inMenuBar:
  72.                     result = MenuSelect(where)
  73.                     id = (result>>16) & 0xffff
  74.                     if id:
  75.                         item = result & 0xffff
  76.                         print "Menu id", id, ", item", item
  77.                         if id == appleid:
  78.                             name = applemenu.GetMenuItemText(item)
  79.                             print name
  80.                             OpenDeskAcc(name)
  81.                     HiliteMenu(0)
  82.                 elif partcode == inGrow:
  83.                     result = win.GrowWindow(where, everywhere)
  84.                     if result:
  85.                         winheight = (result>>16) & 0xffff
  86.                         winwidth = result & 0xffff
  87.                         win.SizeWindow(winwidth, winheight, 0)
  88.                         SetPort(win)
  89.                         InvalRect(everywhere)
  90.                 elif partcode == inContent:
  91.                     local = GlobalToLocal(where)
  92.                     print "local =", local
  93.                     ctltype, control = FindControl(local, win)
  94.                     if ctltype and control:
  95.                         pcode = control.TrackControl(local)
  96.                         if pcode:
  97.                             if control is resize:
  98.                                 if winwidth == 200:
  99.                                     winwidth = 300
  100.                                 else:
  101.                                     winwidth = 200
  102.                                 win.SizeWindow(winwidth, winheight, 0)
  103.                                 SetPort(win)
  104.                                 InvalRect(everywhere)
  105.                             elif control is quit:
  106.                                 break
  107.                     else:
  108.                         print ctltype, control
  109.                 else:
  110.                     print "Mouse down at", where, "(global)"
  111.                     print partcode, win
  112.             elif what == mouseUp:
  113.                 print "Mouse up at", where, "(global)"
  114.             elif what == keyDown:
  115.                 print "Key down:", hex(message), hex(modifiers)
  116.                 char = chr(message & 0xff)
  117.                 keycode = (message & 0xff00) >> 8
  118.                 print 'char:', `char`, '; keycode:', keycode
  119.                 if char == 'Q': break
  120.             elif what == autoKey:
  121.                 print "Key repeat:", hex(message), hex(modifiers)
  122.             elif what == updateEvt:
  123.                 print "update Event", message, modifiers
  124.                 w.BeginUpdate()
  125.                 EraseRect(everywhere)
  126.                 MoveTo(0, 0)
  127.                 LineTo(100, 100)
  128.                 TextSize(9)
  129.                 DrawString("This is a dead parrot!")
  130.                 MoveTo(0, 100)
  131.                 LineTo(100, 0)
  132.                 Move(0, 20)
  133.                 TextSize(12)
  134.                 DrawString("Nobody expects the Spanish Inquisition")
  135.                 TextSize(20)
  136.                 DrawString("!")
  137.                 UpdateControls(w)
  138.                 w.DrawGrowIcon()
  139.                 w.EndUpdate()
  140.             elif what == activateEvt:
  141.                 print "activateEvt:", message, modifiers
  142.                 InitCursor()
  143.             elif what == osEvt:
  144.                 print "osEvt:", message, where, modifiers
  145.             elif what == 23:
  146.                 try:
  147.                     AE.AEProcessAppleEvent(event)
  148.                 except AE.Error, msg:
  149.                     print "AE.Error:", msg
  150.             else:
  151.                 print "???", event
  152.  
  153. main()
  154.